package com.peternguyen.totolistpro.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.peternguyen.totolistpro.data.Category;
import com.peternguyen.totolistpro.sql.TodoSQLiteHelper;

public class CategoryDAO {
	private SQLiteDatabase db;
	private TodoSQLiteHelper dbHelper;
	TodoDAO dao;

	public CategoryDAO(Context context) {
		dbHelper = new TodoSQLiteHelper(context);
		db = dbHelper.getWritableDatabase();
		dao = new TodoDAO(context);
	}

	// Close the db
	public void close() {
		db.close();
	}

	// Create
	public void createCat(Category cat) {
		ContentValues contentValues = new ContentValues();
		contentValues.put("name", cat.get_name());
		contentValues.put("thumb_url", cat.get_img());
		Log.i("Test", "Ten:" + cat.get_name());
		// Insert into DB
		db.insert("tblCategory", null, contentValues);
		db.close();
	}

	// update
	public void UpdatePWD(Category cat) {
		ContentValues contentValues = new ContentValues();
		contentValues.put("name", cat.get_name());
		contentValues.put("thumb_url", cat.get_img());
		Log.i("Test", "Ten:" + cat.get_name());
	
		// Update datatbase
		db.update("tblCategory", contentValues, "_id= " + cat.get_id(), null);
	}

	// Delete
	public int deleteCat(int id) {
		// Delete from DB where id match
		if (dao.countTodo(id) == 0) {
			db.delete("tblCategory", "_id = " + id, null);
			return 1;// Xoa thanh con
		}
		return 0;// Xoa that bai
	}

	// Get Acc

	public List<Category> getList() {
		List<Category> lstcat = new ArrayList<Category>();

		// Name of the columns we want to select
		String[] tableColumns = new String[] { "_id", "name", "thumb_url" };

		// Query the database
		Cursor cursor = db.query("tblCategory", tableColumns, null, null, null,
				null, null);
		cursor.moveToFirst();
		Category cat;
		// Iterate the results
		while (!cursor.isAfterLast()) {
			// Take values from the DB
			cat = new Category();
			cat.set_id(cursor.getInt(0));
			cat.set_name(cursor.getString(1));
			cat.set_img(cursor.getBlob(2));
			lstcat.add(cat);
			cursor.moveToNext();
		}

		return lstcat;
	}

	// getCatnem
	public Category getCategory(int id) {
		// Name of the columns we want to select
		String[] tableColumns = new String[] { "_id", "name", "thumb_url" };

		// Query the database
		Cursor cursor = db.query("tblCategory", tableColumns, "_id=" + id,
				null, null, null, null);
		cursor.moveToFirst();
		Category cat;
		// Iterate the results
		while (!cursor.isAfterLast()) {
			// Take values from the DB
			cat = new Category();
			cat.set_id(cursor.getInt(0));
			cat.set_name(cursor.getString(1));
			cat.set_img(cursor.getBlob(2));
			return cat;
		}
		return null;

	}

	public List<Category> getList(String name) {
		List<Category> lstcat = new ArrayList<Category>();

		// Name of the columns we want to select
		String[] tableColumns = new String[] { "_id", "name", "thumb_url" };

		// Query the database
		Cursor cursor = db.query("tblCategory", tableColumns, "name='" + name
				+ "'", null, null, null, null);
		cursor.moveToFirst();
		Category cat;
		// Iterate the results
		while (!cursor.isAfterLast()) {
			// Take values from the DB
			cat = new Category();
			cat.set_id(cursor.getInt(0));
			cat.set_name(cursor.getString(1));
			cat.set_img(cursor.getBlob(2));
			lstcat.add(cat);
			cursor.moveToNext();
		}

		return lstcat;
	}
}
